package com.suredy.app.porject.model.vo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.suredy.security.entity.UserEntity;

/**
 * 付款计划筛选模型
 * 
 * @author VIVID.G
 * @since 2016-2-24
 * @version v0.1
 */
public class PayoutPlanFilterModel {

	/* 合同编号 */
	private String contractNo;

	/* 合同名称 */
	private String contractName;

	/* 付款单位 */
	private String planToCompany;

	/* 付款金额 */
	private Double planMinOutMoney;

	/* 付款金额 */
	private Double planMaxOutMoney;

	/* 付款占比 */
	private Double planMinOutMoneyRatio;

	/* 付款占比 */
	private Double planMaxOutMoneyRatio;

	/* 付款日期 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMinOutTime;

	/* 付款日期 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMaxOutTime;

	/* 负责人 */
	private String planHeader;

	/* 计划变更 */
	private Boolean planChanged;

	/* 执行情况 */
	private Boolean planPayout;

	public String getContractNo() {
		return contractNo;
	}

	public void setContractNo(String contractNo) {
		this.contractNo = contractNo;
	}

	public String getContractName() {
		return contractName;
	}

	public void setContractName(String contractName) {
		this.contractName = contractName;
	}

	public String getPlanToCompany() {
		return planToCompany;
	}

	public void setPlanToCompany(String planToCompany) {
		this.planToCompany = planToCompany;
	}

	public Double getPlanMinOutMoney() {
		return planMinOutMoney;
	}

	public void setPlanMinOutMoney(Double planMinOutMoney) {
		this.planMinOutMoney = planMinOutMoney;
	}

	public Double getPlanMaxOutMoney() {
		return planMaxOutMoney;
	}

	public void setPlanMaxOutMoney(Double planMaxOutMoney) {
		this.planMaxOutMoney = planMaxOutMoney;
	}

	public Double getPlanMinOutMoneyRatio() {
		return planMinOutMoneyRatio;
	}

	public void setPlanMinOutMoneyRatio(Double planMinOutMoneyRatio) {
		this.planMinOutMoneyRatio = planMinOutMoneyRatio;
	}

	public Double getPlanMaxOutMoneyRatio() {
		return planMaxOutMoneyRatio;
	}

	public void setPlanMaxOutMoneyRatio(Double planMaxOutMoneyRatio) {
		this.planMaxOutMoneyRatio = planMaxOutMoneyRatio;
	}

	public Date getPlanMinOutTime() {
		return planMinOutTime;
	}

	public void setPlanMinOutTime(Date planMinOutTime) {
		this.planMinOutTime = planMinOutTime;
	}

	public Date getPlanMaxOutTime() {
		return planMaxOutTime;
	}

	public void setPlanMaxOutTime(Date planMaxOutTime) {
		this.planMaxOutTime = planMaxOutTime;
	}

	public String getPlanHeader() {
		return planHeader;
	}

	public void setPlanHeader(String planHeader) {
		this.planHeader = planHeader;
	}

	public Boolean getPlanChanged() {
		return planChanged;
	}

	public void setPlanChanged(Boolean planChanged) {
		this.planChanged = planChanged;
	}

	public Boolean getPlanPayout() {
		return planPayout;
	}

	public void setPlanPayout(Boolean planPayout) {
		this.planPayout = planPayout;
	}

	public List<Object> makeQl() {
		String ql = "SELECT T FROM ContractPayoutPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, true);
	}

	public List<Object> makeCountQl() {
		String ql = "SELECT COUNT(T) FROM ContractPayoutPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, false);
	}

	private List<Object> makeWhereQl(String ql, boolean order) {
		List<Object> ret = new ArrayList<Object>();

		List<Object> params = new ArrayList<Object>();

		// 合同编号
		if (!StringUtils.isBlank(this.contractNo)) {
			ql += " AND C.no like ?";
			params.add("%" + this.contractNo + "%");
		}

		// 合同名称
		if (!StringUtils.isBlank(this.contractName)) {
			ql += " AND C.name like ?";
			params.add("%" + this.contractName + "%");
		}

		// 付款单位
		if (!StringUtils.isBlank(this.planToCompany)) {
			ql += " AND T.moneyTo like ?";
			params.add("%" + this.planToCompany + "%");
		}

		// 付款金额
		if (this.planMinOutMoney != null) {
			ql += " AND T.total >= ?";
			params.add(this.planMinOutMoney);
		}

		// 付款金额
		if (this.planMaxOutMoney != null) {
			ql += " AND T.total <= ?";
			params.add(this.planMaxOutMoney);
		}

		// 付款占比
		if (this.planMinOutMoneyRatio != null) {
			ql += " AND (T.total >= (C.price * ?))";
			params.add(this.planMinOutMoneyRatio / 100);
		}

		// 付款占比
		if (this.planMaxOutMoneyRatio != null) {
			ql += " AND (T.total <= (C.price * ?))";
			params.add(this.planMaxOutMoneyRatio / 100);
		}

		// 付款日期
		if (this.planMinOutTime != null) {
			ql += " AND T.payoutTime >= ?";
			params.add(this.planMinOutTime);
		}

		// 付款日期
		if (this.planMaxOutTime != null) {
			ql += " AND T.payoutTime <= ?";
			params.add(this.planMaxOutTime);
		}

		// 负责人
		if (!StringUtils.isBlank(this.planHeader)) {
			ql += " AND T.header = ?";
			params.add(new UserEntity(this.planHeader));
		}

		// 计划变更
		if (this.planChanged != null) {
			// 已变更
			if (this.planChanged)
				ql += " AND T.changed = ?";

			// 未变更
			else
				ql += " AND (T.changed IS NULL OR T.changed = ?)";

			params.add(this.planChanged);
		}

		// 执行情况
		if (this.planPayout != null) {
			// 已付款
			if (this.planPayout)
				ql += " AND T.payout = ?";

			// 未付款
			else
				ql += " AND (T.payout IS NULL OR T.payout = ?)";

			params.add(this.planPayout);
		}

		// 排序
		if (order)
			ql += " ORDER BY T.payout, T.payoutTime";

		ret.add(ql);
		ret.add(params);

		return ret;
	}
}
